<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class RbacForm extends Model
{
    public $role_name;
    public $permission_name;
    public $description;
    public $user_id;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['role_name', 'permission_name','user_id','description'], 'required'],
            // rememberMe must be a boolean value
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_CREATE_PERMISSION] = ['permission_name','description'];
        $scenarios[self::SCENARIO_CREATE_ROLE] = ['role_name','description'];
        $scenarios[self::SCENARIO_CREATE_EMPOWERMENT] = ['role_name','permission_name'];
        $scenarios[self::SCENARIO_ASSIGN] = ['role_name','user_id'];
        return $scenarios;
    }

    public function attributeLabels()
    {
        return [
            'role_name'=>'角色',
            'permission_name'=>'权限',
            'user_id'=>'用户',
            'description'=>'描述',
        ];
    }

    const SCENARIO_CREATE_PERMISSION = 'create-permission';
    const SCENARIO_CREATE_ROLE = 'create-role';
    const SCENARIO_CREATE_EMPOWERMENT = 'create-empowerment';
    const SCENARIO_ASSIGN = 'assign';

    //给用户分配角色
    public function assign()
    {
        $auth = Yii::$app->authManager;
        $reader = $auth->createRole($this->role_name);
        $auth->assign($reader, $this->user_id);
    }
}
